public class mergeTwoLists {
     static class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

    public static void main(String[] args) {
         ListNode a1 = new ListNode(1);
         ListNode a2 = new ListNode(2);
         ListNode a3 = new ListNode(4);
         a1.next=a2;
         a2.next=a3;
         ListNode l1 = a1;
        ListNode b1 = new ListNode(1);
        ListNode b2 = new ListNode(3);
        ListNode b3 = new ListNode(4);
        b1.next=b2;
        b2.next=b3;
        ListNode l2 = b1;
        ListNode l = mergeTwoLists(l1,l2);
        while(l!=null){
            System.out.println(l.val);
        }

    }
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        else if(l2==null) return l1;
        else if(l1.val<l2.val){
            l1.next=mergeTwoLists(l1.next,l2);
            return l1;

        }
        else{
            l2.next=mergeTwoLists(l1,l2.next);
            return l2;
        }
    }
}
